package com.ruoyi.comment.service.impl;

import com.alibaba.fastjson.JSON;
import com.ruoyi.comment.domain.TbComment;
import com.ruoyi.comment.mapper.TbCommentMapper;
import com.ruoyi.comment.service.ITbCommentService;
import com.ruoyi.common.utils.DateUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 评论Service业务层处理
 *
 * @author 劳雅玲
 * @date 2024-03-27
 */
@Service
public class TbCommentServiceImpl implements ITbCommentService {
    @Autowired
    private TbCommentMapper tbCommentMapper;

    @Resource
    private TbCommentAsyncServiceImpl tbCommentAsyncService;

    /**
     * 查询评论
     *
     * @param id 评论主键
     * @return 评论
     */
    @Override
    public TbComment selectTbCommentById(Integer id) {
        return tbCommentMapper.selectTbCommentById(id);
    }

    /**
     * 查询评论列表
     *
     * @param tbComment 评论
     * @return 评论
     */
    @Override
    public List<TbComment> selectTbCommentList(TbComment tbComment) {
        return tbCommentMapper.selectTbCommentList(tbComment);
    }

    /**
     * 新增评论
     *
     * @param tbComment 评论
     * @return 结果
     */
    @Override
    public int insertTbComment(TbComment tbComment) {
        tbComment.setCreateTime(DateUtils.getNowDate());
        return tbCommentMapper.insertTbComment(tbComment);
    }

    /**
     * 修改评论
     *
     * @param tbComment 评论
     * @return 结果
     */
    @Override
    public int updateTbComment(TbComment tbComment) {
        tbComment.setUpdateTime(DateUtils.getNowDate());
        return tbCommentMapper.updateTbComment(tbComment);
    }

    /**
     * 批量删除评论
     *
     * @param ids 需要删除的评论主键
     * @return 结果
     */
    @Override
    public int deleteTbCommentByIds(Integer[] ids) {
        return tbCommentMapper.deleteTbCommentByIds(ids);
    }

    /**
     * 删除评论信息
     *
     * @param id 评论主键
     * @return 结果
     */
    @Override
    public int deleteTbCommentById(Integer id) {
        return tbCommentMapper.deleteTbCommentById(id);
    }

    @Override
    public void dianzan(Integer id) {
        TbComment tbComment = tbCommentMapper.selectTbCommentById(id);

        tbComment.setOkCount(tbComment.getOkCount() + 1);
        tbCommentMapper.updateTbComment(tbComment);

    }

    @Override
    public List<TbComment> listTree(TbComment tbComment) {
        // 找出所有节点
        List<TbComment> tbCommentsRoots = tbCommentMapper.selectTbCommentList(tbComment);
        System.out.println("查询得到的数据="+JSON.toJSONString(tbCommentsRoots));

        List<TbComment> tbComments = recursionFunction(tbCommentsRoots);

        // 筛选出来数据
        List<TbComment> tbComments1 = new ArrayList<>();
        if (!Objects.isNull(tbComment.getId())){
            tbComments.parallelStream().forEach(tbComment1 -> {
                if (tbComment1.getId().equals(tbComment.getId())){
                    tbComments1.add(tbComment1);
                }
            });
            System.out.println("筛选得到的数据="+JSON.toJSONString(tbComments1));
            return CollectionUtils.isEmpty(tbComments1.get(0).getChildren())?new ArrayList<TbComment>():tbComments1.get(0).getChildren();
        }else {

            return tbComments;
        }
    }

    public static List<TbComment> recursionFunction(List<TbComment> list) {
        // 使用HashMap存储所有节点，便于快速查找
        Map<Integer, TbComment> nodeMap = new HashMap<>();
        for (TbComment comment : list) {
            nodeMap.put(comment.getId(), comment);
        }

        // 遍历列表，将节点加入其父节点的子列表中
        List<TbComment> roots = new ArrayList<>();
        for (TbComment comment : list) {
            if (comment.getParentId() == 0) {
                roots.add(comment);
            } else {
                TbComment parent = nodeMap.get(comment.getParentId());
                if (parent != null ) {
                    if (parent.getChildren() == null) {
                        parent.setChildren(new ArrayList<>());
                    }
                    parent.getChildren().add(comment);
                    nodeMap.put(parent.getId(), parent);
                }
            }
        }
        // 返回根节点组成的森林结构
        return roots;
    }

}
